﻿using System;
using System.Collections.Generic;
using System.Linq;
using CompactMapper.Orm.Contracts;
using CompactMapper.Orm.UnitOfWork;
using CompactMapper.Orm.UnitOfWork.Mappings;

namespace CompactMapper.Orm.Lookups
{
    public sealed class LookupTable<T> : ILookupTable<T>
        where T : class
    {
        private readonly String _connectionString;
        private readonly IMappings _mappings;

        internal LookupTable(string connectionString, IMappings mappings)
        {
            if (mappings == null)
            {
                throw new ArgumentNullException("mappings");
            }
            _mappings = mappings;

            if (connectionString == null)
            {
                throw new ArgumentNullException("connectionString");
            }
            _connectionString = connectionString;
        }

        public IEnumerable<T> GetAll()
        {
            using (var connectionWrapper = ConnectionFactory.CreateConnection(_connectionString))
            {
                return _mappings.RunGetAll<T>(connectionWrapper.Connection).ToArray();
            }
        }
    }
}